/**
 * 获取分类信息插件  
 * @author Max.Yu <max@jipu.com>
 * @version 2015041517
 * @example 
 * $('#item_type_div').loadtype({
      type: 'area',  //【商品分类】itemCategory | 【地区】area
      name1: 'prov',
      name2: 'city',
      name3: 'area',
      value1: 230000,
      value2: 230700,
      value3: 230703
    });
 **/
(function($){
  $.fn.extend({
    loadtype : function(options){
      //定义默认参数
      var defaults = {
        type: 'itemCategory',
        store_id: '',
        name1: 'cid_1',
        name2: 'cid_2',
        name3: 'cid_3',
        value1: '',
        value2: '',
        value3: '',
        changed:function(){}
      };
      var data_init = function(obj, options, data){
        var rand = Math.random()* 1e17;
        var select_default = '<option value="">请选择</option>';
        var html1 = html2 = html3 = select_default;
        if(data[0]){
          for(var i = 0; i <data[0].length; i++){
            html1 += '<option value="'+ data[0][i].id +'">'+ data[0][i].name +'</option>';
          }
        }
        var initHtml = '';
        initHtml = '<select name="'+ options.name1 +'" id="n1_'+ rand +'">' + html1 + '</select>\r\n';
        initHtml += '<select name="'+ options.name2 +'" id="n2_'+ rand +'" style="display:none;">'+ html2 +'</select>\r\n';
        initHtml += '<select name="'+ options.name3 +'" id="n3_'+ rand +'" style="display:none;">'+ html3 +'</select>\r\n';
        $(obj).html(initHtml);
        var changed = function(){
          if(typeof options.changed === 'function'){
            options.changed($('#n1_'+rand).val(), $('#n2_'+rand).val(), $('#n3_'+rand).val());
          }
        };
        var s="";
        $('#n1_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n2_' + rand).html(select_default).hide();
          $('#n3_' + rand).html(select_default).hide();

          if($("#temp")){
            $('#n1_' + rand+">option:selected").each(function (index,el){
                 if($(this).text()!="请选择") s+=$(this).text()+",";
             })
          }

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html2 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html2 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n2_' + rand).html(html2).show();
            }
          }
          if($("#temp").length>0) {
            $("#temp").val(s)
            var c_array = [];
            $('select[name="cid_1"]').val()!="" && c_array.push($('select[name="cid_1"] option:selected').text());
            $('select[name="cid_2"]').val()!="" && c_array.push($('select[name="cid_2"] option:selected').text());
            $('select[name="cid_3"]').val()!="" && c_array.push($('select[name="cid_3"] option:selected').text());
            category=c_array.join('/');
            url = '/Shop/ItemPublicCategory/getContent?category='+category+'&cid_1=' + $('#n1_' + rand+">option:selected").val() + '&cid_2=' +$('#n2_' + rand+">option:selected").val() ;
            $.get(url,function(data){
              if(data != null){
                $('#content').val(data.content);
              }
            },"json");
          }
          changed();
        });

        $('#n2_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n3_' + rand).html(select_default).hide();
          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html3 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html3 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n3_' + rand).html(html3).show();
            }
          }
          if($("#temp").length>0) {
            $("#temp").val(s)
            var c_array = [];
            $('select[name="cid_1"]').val()!="" && c_array.push($('select[name="cid_1"] option:selected').text());
            $('select[name="cid_2"]').val()!="" && c_array.push($('select[name="cid_2"] option:selected').text());
            $('select[name="cid_3"]').val()!="" && c_array.push($('select[name="cid_3"] option:selected').text());
            category=c_array.join('/');
            url = '/Shop/ItemPublicCategory/getContent?category='+category+'&cid_1=' + $('#n1_' + rand+">option:selected").val() + '&cid_2=' + $('#n2_' + rand+">option:selected").val() ;
            $.get(url,function(data){
              if(data != null){
                $('#content').val(data.content);
              }
            },"json");
          }
          changed();
        });

        $('#n3_' + rand).change(function(){
          s="";
          $("#temp").val(s)
          changed();
        });


        if(options.value1 !== ''){
          $('#n1_' + rand+' option[value="'+ options.value1 +'"]').attr('selected', true);
          $('#n1_' + rand).change();
        }
        if(options.value2 !== ''){
          $('#n2_' + rand+' option[value="'+ options.value2 +'"]').attr('selected', true);
          $('#n2_' + rand).change();
        }
        if(options.value3 !== ''){
          $('#n3_' + rand+' option[value="'+ options.value3 +'"]').attr('selected', true);
          $('#n3_' + rand).change();
        }

      };
      //合并参数
      options = $.extend(defaults, options);
      this.each(function(){
        var _self = this;
        //处理插件逻辑
        try{
          var type_data = eval(options.type);
          data_init(_self, options, type_data);
        }catch(e){
          $.getScript(Core.U('/Api/getTypeData/', 'type=' + options.type +'&store_id='+options.store_id+"&r="+Math.random()), function(){
            data_init(_self, options, eval(options.type));
          });
        }
      });
    },
    loadtype1 : function(options){

      //定义默认参数 抖音
      var defaults = {
        type: 'itemCategory_dy',
        store_id: '',
        name1: 'cid_1_dy',
        name2: 'cid_2_dy',
        name3: 'cid_3_dy',
        name4: 'cid_4_dy',
        name5: 'cid_5_dy',
        value1: '',
        value2: '',
        value3: '',
        value4: '',
        value5: '',
        changed:function(){}
      };
      var data_init = function(obj, options, data){
        var rand = Math.random()* 1e17;
        var select_default = '<option value="">请选择</option>';
        var html1 = html2 = html3 = html4 = html5 = select_default;
        if(data[0]){
          for(var i = 0; i <data[0].length; i++){
            html1 += '<option value="'+ data[0][i].id +'">'+ data[0][i].name +'</option>';
          }
        }
        var initHtml = '';
        initHtml = '<select name="'+ options.name1 +'" id="n1_'+ rand +'">' + html1 + '</select>\r\n';
        initHtml += '<select name="'+ options.name2 +'" id="n2_'+ rand +'" style="display:none;">'+ html2 +'</select>\r\n';
        initHtml += '<select name="'+ options.name3 +'" id="n3_'+ rand +'" style="display:none;">'+ html3 +'</select>\r\n';
        initHtml += '<select name="'+ options.name4 +'" id="n4_'+ rand +'" style="display:none;">'+ html4 +'</select>\r\n';
        initHtml += '<select name="'+ options.name5 +'" id="n5_'+ rand +'" style="display:none;">'+ html5 +'</select>\r\n';
        $(obj).html(initHtml);
        var changed = function(){
          if(typeof options.changed === 'function'){
            options.changed($('#n1_'+rand).val(), $('#n2_'+rand).val(), $('#n3_'+rand).val(), $('#n4_'+rand).val(), $('#n5_'+rand).val());
          }
        };
        var s="";
        $('#n1_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n2_' + rand).html(select_default).hide();
          $('#n3_' + rand).html(select_default).hide();
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html2 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html2 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n2_' + rand).html(html2).show();
            }
          }
          changed();
        });

        $('#n2_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n3_' + rand).html(select_default).hide();
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();
          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html3 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html3 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n3_' + rand).html(html3).show();
            }
          }
          changed();
        });

        $('#n3_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html4 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html4 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n4_' + rand).html(html4).show();
            }
          }
          changed();
        });

        $('#n4_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n5_' + rand).html(select_default).hide();

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html5 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html5 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n5_' + rand).html(html5).show();
            }
          }
          changed();
        });

        if(options.value1 !== ''){
          $('#n1_' + rand+' option[value="'+ options.value1 +'"]').attr('selected', true);
          $('#n1_' + rand).change();
        }
        if(options.value2 !== ''){
          $('#n2_' + rand+' option[value="'+ options.value2 +'"]').attr('selected', true);
          $('#n2_' + rand).change();
        }
        if(options.value3 !== ''){
          $('#n3_' + rand+' option[value="'+ options.value3 +'"]').attr('selected', true);
          $('#n3_' + rand).change();
        }
        if(options.value4 !== ''){
          $('#n4_' + rand+' option[value="'+ options.value4 +'"]').attr('selected', true);
          $('#n4_' + rand).change();
        }
        if(options.value5 !== ''){
          $('#n5_' + rand+' option[value="'+ options.value5 +'"]').attr('selected', true);
          $('#n5_' + rand).change();
        }
      };



      //合并参数
      options = $.extend(defaults, options);

      this.each(function(){
        var _self = this;
        //处理插件逻辑
        try{
          var type_data = eval(options.type);
          data_init(_self, options, type_data);
        }catch(e){
          $.getScript(Core.U('/Api/getTypeData/', 'type=' + options.type +'&store_id='+options.store_id+"&r="+Math.random()), function(){
            data_init(_self, options, eval(options.type));
          });
        }
      });
    },
    loadtype2 : function(options){

      //定义默认参数 抖音
      var defaults = {
        type: 'itemCategory_sph',
        store_id: '',
        name1: 'cid_1_sph',
        name2: 'cid_2_sph',
        name3: 'cid_3_sph',
        name4: 'cid_4_sph',
        name5: 'cid_5_sph',
        value1: '',
        value2: '',
        value3: '',
        value4: '',
        value5: '',
        changed:function(){}
      };
      var data_init = function(obj, options, data){
        var rand = Math.random()* 1e17;
        var select_default = '<option value="">请选择</option>';
        var html1 = html2 = html3 = html4 = html5 = select_default;
        if(data[0]){
          for(var i = 0; i <data[0].length; i++){
            html1 += '<option value="'+ data[0][i].id +'">'+ data[0][i].name +'</option>';
          }
        }
        debugger;
        var initHtml = '';
        initHtml = '<select name="'+ options.name1 +'" id="n1_'+ rand +'">' + html1 + '</select>\r\n';
        initHtml += '<select name="'+ options.name2 +'" id="n2_'+ rand +'" style="display:none;">'+ html2 +'</select>\r\n';
        initHtml += '<select name="'+ options.name3 +'" id="n3_'+ rand +'" style="display:none;">'+ html3 +'</select>\r\n';
        initHtml += '<select name="'+ options.name4 +'" id="n4_'+ rand +'" style="display:none;">'+ html4 +'</select>\r\n';
        initHtml += '<select name="'+ options.name5 +'" id="n5_'+ rand +'" style="display:none;">'+ html5 +'</select>\r\n';
        $(obj).html(initHtml);
        var changed = function(){
          if(typeof options.changed === 'function'){
            options.changed($('#n1_'+rand).val(), $('#n2_'+rand).val(), $('#n3_'+rand).val(), $('#n4_'+rand).val(), $('#n5_'+rand).val());
          }
        };
        var s="";
        $('#n1_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n2_' + rand).html(select_default).hide();
          $('#n3_' + rand).html(select_default).hide();
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html2 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html2 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n2_' + rand).html(html2).show();
            }
          }
          changed();
        });

        $('#n2_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n3_' + rand).html(select_default).hide();
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();
          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html3 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html3 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n3_' + rand).html(html3).show();
            }
          }
          changed();
        });

        $('#n3_' + rand).change(function(){
          s="";
          var value = $(this).val() === '' ? NaN : $(this).val() * 1;
          $('#n4_' + rand).html(select_default).hide();
          $('#n5_' + rand).html(select_default).hide();

          if(value !== NaN){
            if(typeof data[value] !== 'undefined'){
              html4 = select_default;
              for(var i = 0; i < data[value].length; i++){
                html4 += '<option value="'+ data[value][i].id +'">'+ data[value][i].name +'</option>';
                s+=data[value][i].name+",";
              }
              $('#n4_' + rand).html(html4).show();
            }
          }
          changed();
        });

        if(options.value1 !== ''){
          $('#n1_' + rand+' option[value="'+ options.value1 +'"]').attr('selected', true);
          $('#n1_' + rand).change();
        }
        if(options.value2 !== ''){
          $('#n2_' + rand+' option[value="'+ options.value2 +'"]').attr('selected', true);
          $('#n2_' + rand).change();
        }
        if(options.value3 !== ''){
          $('#n3_' + rand+' option[value="'+ options.value3 +'"]').attr('selected', true);
          $('#n3_' + rand).change();
        }
        if(options.value4 !== ''){
          $('#n4_' + rand+' option[value="'+ options.value4 +'"]').attr('selected', true);
          $('#n4_' + rand).change();
        }

      };

      //合并参数
      options = $.extend(defaults, options);

      this.each(function(){
        var _self = this;
        //处理插件逻辑
        try{
          var type_data = eval(options.type);
          data_init(_self, options, type_data);
        }catch(e){
          $.getScript(Core.U('/Api/getTypeData/', 'type=' + options.type +'&store_id='+options.store_id+"&r="+Math.random()), function(){
            data_init(_self, options, eval(options.type));
          });
        }
      });
    }


  });
})(jQuery);